package com.elong.android.specialhouse.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.entity.HouseOrderEntity;
import com.elong.android.specialhouse.utils.DateTimeUtils;
import com.elong.android.specialhouse.utils.ImageLoaderUtils;
import com.elong.android.specialhouse.utils.StringUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class MyOrderAdapter extends BaseAdapter {

    private final static int TAG_ORDER = R.id.tag_order;
    private Context mContext;
    private List<HouseOrderEntity> mData;
    public final static int MY_ORDER_STATUS_NEW_ORDER = 0;
    public final static int MY_ORDER_STATUS_CONNECT_ORDER = 1;
    public final static int MY_ORDER_STATUS_RESERVE_SUCCEED = 2;
    public final static int MY_ORDER_STATUS_CANCEL_ORDER = 5;
    public final static int MY_ORDER_STATUS_ALREADY_CHECKIN = 6;
    public final static int MY_ORDER_STATUS_ORDER_FINISH = 10;
    private HouseOrderItemClickListener listener;

    // 图片异步加载工具
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;

    // Item点击事件回调
    public MyOrderAdapter(Context context, List<HouseOrderEntity> data) {
        mContext = context;
        mData = data;
        this.options = ImageLoaderUtils.getHouseImageOptions();
    }

    /**
     * 设置Item点击监听
     *
     * @param listener
     */
    public void setHouseOrderItemClickListener(HouseOrderItemClickListener listener) {
        this.listener = listener;
    }

    /**
     * 设置Adapter数据
     *
     * @param data      数据集合
     * @param isAddMode 增加方式 true:设置前不清除原有数据 false:先清空原有数据在添加
     */
    public void setData(List<HouseOrderEntity> data, boolean isAddMode) {
        if (!isAddMode) {
            mData.clear();
        }
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public List<HouseOrderEntity> getData() {
        return mData;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OrderListItemView view;
        if (convertView == null) {
            view = new OrderListItemView(mContext);
            addListener(view);
        } else {
            view = (OrderListItemView) convertView;
        }

        HouseOrderEntity order = mData.get(position);
        if (order == null) {
            return view;
        }
        view.tvMyOrderStatus.setText(order.getOrderStatusDesc());
        view.tvMyOrderHouseName.setText(order.getApartmentName());
        String checkintime = DateTimeUtils.changeDateFormat(order.ArriveDate);
        String leavetime = DateTimeUtils.changeDateFormat(order.LeaveDate);
        if(!DateTimeUtils.isDateInOneYear(order.ArriveDate,order.LeaveDate,"yyyy-MM-dd")){
            //入离店日期不在同一年时，都显示年
            checkintime = DateTimeUtils.changeDateFormatContainsYear(order.ArriveDate);
            leavetime = DateTimeUtils.changeDateFormatContainsYear(order.LeaveDate);
        }
        StringBuffer time = new StringBuffer();
        time.append(checkintime);
        time.append("-");
        if( !StringUtils.isEmpty(leavetime) &&
                leavetime.contains("年")){
            time.append("\n");//离店日期包含年时需要换行显示
        }
        time.append(leavetime);
        view.tvMyOrderCheckinDate.setText(time.toString());
//        view.tvMyOrderPrice.setText(StringUtils.toRoundInt(order.getGorderAmount()));
        view.tvMyOrderPrice.setText(order.getGorderAmount());
        view.tvMyOrderCheckinPeopleNum.setText("人数：" + order.getCustomCount() + "个客人");
        BtnStatus(order, view);
        return view;
    }

    private void addListener(OrderListItemView view) {
        view.tvMyOrderCancel.setOnClickListener(new HouseOrderClickListener());
        view.tvMyOrderReminder.setOnClickListener(new HouseOrderClickListener());
        view.tvMyOrderConfirm.setOnClickListener(new HouseOrderClickListener());
        view.tvMyOrderToComment.setOnClickListener(new HouseOrderClickListener());
        view.tvMyOrderToPay.setOnClickListener(new HouseOrderClickListener());

    }

    class HouseOrderClickListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            HouseOrderEntity order = (HouseOrderEntity) v.getTag(TAG_ORDER);
            switch (v.getId()) {
                case R.id.tv_my_order_cancel:
                    listener.onClick_cancel(order);
                    break;
                case R.id.tv_my_order_reminder:
                    listener.onClick_reminder(order);
                    break;
                case R.id.tv_my_order_confirm:
                    listener.onClick_confirm(order);
                    break;
                case R.id.tv_my_order_to_comment:
                    listener.onClick_to_comment(order);
                    break;
                case R.id.tv_my_order_to_pay:
                    listener.onClick_to_pay(order);
                    break;
//                case R.id.tv_contact_he:
//                    listener.onClick_contact_he(order);
//                    break;
                default:
                    break;
            }
        }
    }

    /**
     * @param @param order
     * @param @param view
     * @return void
     * @throws
     * @Title: BtnStatus
     * @Description: TODO 设置房客btn状态
     */
    private void BtnStatus(HouseOrderEntity order, OrderListItemView view) {
        boolean isShowBtn = false;
        int OrderStatus = order.getOrderStatus();
        if (order.getIsCanCancel() == true) {
            view.tvMyOrderCancel.setVisibility(View.VISIBLE);
            view.tvMyOrderCancel.setTag(TAG_ORDER, order);
            isShowBtn = true;
        } else {
            view.tvMyOrderCancel.setVisibility(View.GONE);
        }

        if (order.getUrgeBttn()) {
            view.tvMyOrderReminder.setVisibility(View.VISIBLE);
            view.tvMyOrderReminder.setTag(TAG_ORDER, order);
            isShowBtn = true;
        } else {
            view.tvMyOrderReminder.setVisibility(View.GONE);
        }

        if (order.getCommentBttn()) {
            view.tvMyOrderToComment.setVisibility(View.VISIBLE);
            view.tvMyOrderToComment.setTag(TAG_ORDER, order);
            isShowBtn = true;
        } else {
            view.tvMyOrderToComment.setVisibility(View.GONE);
        }

        if (order.getPayBttn()) {
            view.tvMyOrderToPay.setVisibility(View.VISIBLE);
            view.tvMyOrderToPay.setTag(TAG_ORDER, order);
            isShowBtn = true;
        } else {
            view.tvMyOrderToPay.setVisibility(View.GONE);
        }
		if(order.getConfirmCheckInBttn()){
			view.tvMyOrderConfirm.setVisibility(View.VISIBLE);
			view.tvMyOrderConfirm.setTag(TAG_ORDER, order);
            isShowBtn = true;
		}else {
			view.tvMyOrderConfirm.setVisibility(View.GONE);
		}

        if(isShowBtn){
            view.splitLine.setVisibility(View.VISIBLE);
        }else{
            view.splitLine.setVisibility(View.GONE);
        }
    }

    class OrderListItemView extends LinearLayout {
        public View splitLine;
        public TextView tvMyOrderStatus;
        public TextView tvMyOrderHouseName;
        public TextView tvMyOrderCheckinDate;
        public TextView tvMyOrderPrice;
        public TextView tvMyOrderCheckinPeopleNum;
        public Button tvMyOrderCancel;
        public Button tvMyOrderReminder;
        public Button tvMyOrderConfirm;
        public Button tvMyOrderToComment;
        public Button tvMyOrderToPay;
        public OrderListItemView(Context context) {
            super(context);
            LayoutInflater.from(context).inflate(R.layout.item_my_order, this);
            initView();
        }

        private void initView() {
            splitLine = findViewById(R.id.view_split_line);
            tvMyOrderStatus = (TextView) findViewById(R.id.tv_my_order_status);
            tvMyOrderHouseName = (TextView) findViewById(R.id.tv_my_order_house_name);
            tvMyOrderCheckinDate = (TextView) findViewById(R.id.tv_my_order_checkin_date);
            tvMyOrderPrice = (TextView) findViewById(R.id.tv_my_order_price);
            tvMyOrderCheckinPeopleNum = (TextView) findViewById(R.id.tv_my_order_checkin_people_num);
            tvMyOrderCancel = (Button) findViewById(R.id.tv_my_order_cancel);
            tvMyOrderReminder = (Button) findViewById(R.id.tv_my_order_reminder);
            tvMyOrderConfirm = (Button) findViewById(R.id.tv_my_order_confirm);
            tvMyOrderToComment = (Button) findViewById(R.id.tv_my_order_to_comment);
            tvMyOrderToPay = (Button) findViewById(R.id.tv_my_order_to_pay);
            splitLine.setVisibility(View.GONE);
            tvMyOrderCancel.setVisibility(View.GONE);
            tvMyOrderReminder.setVisibility(View.GONE);
            tvMyOrderConfirm.setVisibility(View.GONE);
            tvMyOrderToComment.setVisibility(View.GONE);
            tvMyOrderToPay.setVisibility(View.GONE);
        }
    }

    public interface HouseOrderItemClickListener {
        public void onClick_cancel(HouseOrderEntity order);

        public void onClick_contact_he(HouseOrderEntity order);

        public void onClick_to_pay(HouseOrderEntity order);

        public void onClick_to_comment(HouseOrderEntity order);

        public void onClick_confirm(HouseOrderEntity order);

        public void onClick_reminder(HouseOrderEntity order);

    }
}
